package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyList2 extends ListActivity implements Runnable, AdapterView.OnItemClickListener {

    private static final String TAG = "mylist2";
    private Handler handler;
    private SimpleAdapter listitemAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_my_list2);
        //静态列表
        //初始化数据项
        ArrayList<HashMap<String, String>> ListItems = new ArrayList<HashMap<String, String>>();
//        for (int i = 0; i < 30; i++) {
//            HashMap<String, String> map = new HashMap<String, String>(); //map一定要放在循环里面
//            map.put("ItemTitle", "Rate" + i); //标题文字
//            map.put("ItemDetail", "detail" + i); //详细内容
//            ListItems.add(map);  //不要忘记
//        }
//        //生成适配器的Item 和动态数组对应的元素
//        SimpleAdapter listitemAdapter = new SimpleAdapter(this,
//                ListItems, //数据源
//                R.layout.list_items, //Listitems的XML布局实现
//                new String[]{"ItemTitle", "ItemDetail"},
//                new int[]{R.id.Title, R.id.Detail});

        this.setListAdapter(listitemAdapter);
        Thread t = new Thread(this); // 创建新线程
        t.start(); // 开启线程
//        //绑定控件**
//        ListView listview = findViewById( R.id.mylist2);
//        listview.setAdapter(listitemAdapter);
         handler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 7) {
                    List<HashMap<String, String>> list2 = (List<HashMap<String, String>>) msg.obj;
                    //构造适配器对象
                    SimpleAdapter listitemAdapter = new SimpleAdapter(MyList2.this,
                            list2, //数据源
                            R.layout.list_items, //Listitems的XML布局实现
                            new String[]{"ItemTitle", "ItemDetail"},
                            new int[]{R.id.Title, R.id.Detail});
                    setListAdapter(listitemAdapter);
                }
                super.handleMessage(msg);
            }
        };
        getListView().setOnItemClickListener(this); //获取当前列表对象
    }
        @Override
        public void run() {
            //获取网络数据
            Log.i(TAG, "run: run()......");
            for (int i = 1; i <= 3; i++) {
                Log.i(TAG, "run: i=" + i);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            List<HashMap<String, String>> retList = new ArrayList<HashMap<String, String>>();
            try {
                Document doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();  //document必须选择jsoup版

                Elements tables = doc.getElementsByTag("table");
                Element table = tables.get(1);  //注意是从零开始的
//            Log.i(TAG, "run: table=" + table);
                Elements tds = table.getElementsByTag("td");  //按标志td查找

                for (int i = 0; i < tds.size(); i += 8) {
                    //提取数据
                    Element td1 = tds.get(i); //名称
                    Element td2 = tds.get(i + 5);  //汇率值

                    Log.i(TAG, "run" + td1.text() + "-->" + td2.text());
                    String str = td1.text();  //!!将td1也要转成字符否则无法识别
                    String r = td2.text(); //将td2转成String类型

                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("ItemTitle", str);
                    map.put("ItemDetail", r);
                    retList.add(map);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            Message msg = handler.obtainMessage(7);
            msg.obj = retList;  //放入List带回
            handler.sendMessage(msg);
        }

    //当行数据被点击时会调用此方法
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        Log.i(TAG,"adapterView" + adapterView);
        Log.i(TAG,"view" + view);
        Log.i(TAG,"position" + position);
        Log.i(TAG,"id" + id);
        //获得行数据项
        HashMap<String, String> map = (HashMap<String, String>)getListView().getItemAtPosition(position);
        String title = map.get("ItemTitle");
        String detail = map.get("ItemDetail");
        //也可以从view中获得
        //从View中获取选中数据
        TextView Title = (TextView) view.findViewById(R.id.Title);
        TextView Detail = (TextView) view.findViewById(R.id.Detail);
        String title2 = String.valueOf(Title.getText());
        String detail2 = String.valueOf(Detail.getText());

        //打开新的页面，传入参数
        Intent ratedetail = new Intent(this,RateConvert.class);
        ratedetail.putExtra("title",title);
        ratedetail.putExtra("rate",Float.parseFloat(detail));
        startActivity(ratedetail);

    }
}